package app.database.friendlist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class FriendListDB {
	private SQLiteDatabase db;
	private final Context context;
	private final FriendListDBhelper dbhelper;
	
	public FriendListDB(Context c){
		context = c;
		dbhelper = new FriendListDBhelper(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);
	}
	
	public void close(){
		db.close();
	}
	
	public void open() throws SQLiteException {
		try{
			db = dbhelper.getWritableDatabase();
		}catch(SQLiteException ex){
			Log.v("Open database exceptiion caught", ex.getMessage());
			db = dbhelper.getReadableDatabase();
		}
	}
	
	public long insertFriend(String name, String email, String notification, String notificationDate, int table){
		try{
			ContentValues newTaskValue = new ContentValues();
			newTaskValue.put(Constants.FRIEND_NAME, name);
			newTaskValue.put(Constants.EMAIL_NAME, email);
			newTaskValue.put(Constants.NOTIFICATION_NAME, notification);
			newTaskValue.put(Constants.DATE_NAME, notificationDate);
			if(table==0)
				return db.insert(Constants.TABLE_NAME, null, newTaskValue);
			else if(table==1)
				return db.insert(Constants.TABLE_NAME1, null, newTaskValue);
			return -1;
		}catch(SQLiteException ex){
			Log.v("Insert into database exception caught", ex.getMessage());
			return -1;
		}
	}
	
	public long updateFriend(String name, String email, String notification, String notificationDate, String oldemail, int table){
		try{
			ContentValues newTaskValue = new ContentValues();
			newTaskValue.put(Constants.FRIEND_NAME, name);
			newTaskValue.put(Constants.EMAIL_NAME, email);		
			newTaskValue.put(Constants.NOTIFICATION_NAME, notification);
			newTaskValue.put(Constants.DATE_NAME, notificationDate);
			if(table==0)
				return db.update(Constants.TABLE_NAME, newTaskValue, Constants.EMAIL_NAME+"=?", new String[]{oldemail});
			else if(table==1)
				return db.update(Constants.TABLE_NAME1, newTaskValue, Constants.EMAIL_NAME+"=?", new String[]{oldemail});
			return -1;
		}catch(SQLiteException ex){
			return -1;
		}
	}
	
	public long deleteFriend(String oldemail, int table){
		try{
			if(table==0)
				return db.delete(Constants.TABLE_NAME, Constants.EMAIL_NAME+"=?", new String[]{oldemail});
			else if(table==1)
				return db.delete(Constants.TABLE_NAME1, Constants.EMAIL_NAME+"=?", new String[]{oldemail});
			return -1;
		}catch(SQLiteException ex){
			return -1;
		}
	}
	
	public int deleteAllFriends(int table){
		if(table==0)
			return db.delete(Constants.TABLE_NAME, null, null);
		else if(table==1)
			return db.delete(Constants.TABLE_NAME1, null, null);
		return -1;
	}
	
	public Cursor getFriends(int table)
	{
		Cursor c = null;
		if(table==0)
			c = db.query(Constants.TABLE_NAME, null, null, null, null, null, null);
		else if(table==1)
			c = db.query(Constants.TABLE_NAME1, null, null, null, null, null, null);
		return c;
	}
}
